define(function () {

    var self;
    var mask = mui.createMask();
    var search={
        keyword:''
    };

    app.newModule({
        init:function () {
            self = this;
            self.productData = [];
            self.mData = {
                orderSet: {},
                form:{
                    MMRT007: '',//需求日期
                    MMRT012: '',//单号
                    MMRT013: ''//客户
                }
            };
            self._super({
                _tag: 'p_wdbj', // key, 对象放进pageModule
                _sliderId: 'slider', // tabs
                _refreshAutoInit: true, //自动加载刷新控件
                _readyBefore:function () {
                }
            }, win);

        },
        _search:function(d){
            search.keyword = d.keyword.trim();
            self._refresh();
        },
        _refreshCb:function(goods){
            var _super = this._super;
            app.ajax({
                xurl: 'api/wx/wdbj/getPartListData',
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit,
                    keyword: search.keyword
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data;
                        console.log(data)
                        goods.data.pushArray(data.addMap({checked: false,MMRT006:1}));
                        _super({
                            obj: goods,
                            dataLen: data.length,
                            nodata: '无备件...'
                        });
                    }
                }
            });
        },
        _ready:function(){
            Vue.component('wdbj-listitem', {
                template: app.getByid(doc, 'item-template'),
                props: {
                    model: Array
                },
                methods: {
                    minus: function (obj) {
                        if (obj.MMRT006 > 1) {
                            obj.MMRT006--;
                        }
                    },
                    plus:function (obj) {
                        if(obj.MMRT006<obj.MMRS008){
                            obj.MMRT006++;
                        }
                    },
                    showImgs: function (i) {
                        var data = self.mData.orderSet['scroll'].data;
                        var imgs = [];
                        for (var j = 0, k = data.length; j < k; j++) {
                            imgs.push({src: app.getImgUrl()+data[j].GWAM015.split(',')[0]});
                        }
                        app.openPage({
                            src: 'base/previewImages.html',
                            args: {
                                showIndex: i,
                                imgs: imgs
                            }
                        });
                    },
                    changed:function (value) {
                        var goods = self.mData.orderSet['scroll'];
                        var count = 0;
                        goods.data.forEach(function (v) {
                            if(v.checked == value && value ==true){
                                count ++;
                            }
                        });
                        if(count == goods.data.length){
                            if(value){
                                $('#wdbj_index input[type=checkbox]').prop('checked', value);

                            }else {
                                $('#wdbj_index input[type=checkbox]').prop('checked', !value);
                            }
                        }else{
                            if(value){
                                $('#wdbj_index input[type=checkbox]').prop('checked', !value);
                            }else {
                                $('#wdbj_index input[type=checkbox]').prop('checked', value);
                            }
                        }
                    }
                },
                filters: {
                    priceFMT: function (n) {
                        return app.moneyFormat(n);
                    }
                }
            });

            new Vue({
                el: document.getElementById("apply"),
                data: {
                    info: self.mData
                },
                methods: {
                    submit: function () {
                        mask.close();
                        $(".modal1").css("display", "none");
                        for(var i=0;i<self.productData.length;i++){
                            // self.productData[i].MMRT013 = self.mData.form.MMRT013;
                            // self.productData[i].MMRT012 = self.mData.form.MMRT012;
                            self.productData[i].MMRT007 = self.mData.form.MMRT007;
                            self.productData[i].MMRT009 = self.mData.form.MMRT009;
                            self.productData[i].MMRT010 = self.mData.form.MMRT010;
                            self.productData[i].MMRT011 = self.mData.form.MMRT011;

                        }
                        app.openPage({
                            src: 'wdbj/wdbj-play.html',
                            argsKey: 'wdbj-play',
                            args: self.productData
                        });


                    },
                    cancel: function () {
                        mask.close();
                        $(".modal1").css("display", "none");
                    },
                }
            });

            search = new Vue({
                el: doc.getElementById('searchDiv'),
                data: {
                    keyword: search.keyword
                },
                methods:{
                    toSearch:function (e) {
                        var goods = self.mData.orderSet['scroll'];
                        console.log(goods)
                        self._refreshCb(goods);

                        //refreshData(0);
                    }
                }
            });

            var refreshData = function () {
                var goods = self.mData.orderSet['scroll'];
                if (!goods) {
                    goods = self.mData.orderSet['scroll'] = {
                        page: 0,
                        limit: 15,
                        data: [],
                        start: function () {
                            return this.page * this.limit;
                        }
                    };

                    new Vue({
                        el: doc.getElementById('scroll'),
                        data: {
                            items: goods.data
                        }
                    });

                    new Vue({
                        el:doc.getElementById('wdbj_index'),
                        data: {
                            items: goods.data
                        },
                        methods:{
                            bhsq: function () {
                                self.productData.splice(0,self.productData.length);
                                var goods = self.mData.orderSet['scroll'];
                                for(var i=0;i<goods.data.length;i++){
                                    if(goods.data[i].checked == true){
                                        if(goods.data[i].MMRS008<=0){
                                            app.toast('NO.'+(i+1)+'库存不足');
                                            return;
                                        }else{
                                            self.productData.push(goods.data[i]);
                                        }

                                    }
                                }

                                if(self.productData.length == 0){
                                    app.toast('没有选择的备件');
                                }else{
                                    $(".modal1").css("display", "block");
                                    mask.show();
                                }
                            },
                            allcheck:function () {
                                var goods = self.mData.orderSet['scroll'];
                                if($('#allCheck').is(':checked')){
                                    $('#wdbj_index input[type=checkbox]').prop('checked', false);
                                    for(var i=0;i<goods.data.length;i++){
                                        goods.data[i].checked = false;
                                    }
                                }else{
                                    $('#wdbj_index input[type=checkbox]').prop('checked', true);
                                    for(var i=0;i<goods.data.length;i++){
                                        goods.data[i].checked = true;
                                    }
                                }
                            }
                        }
                    })

                    goods._tip = $('#scroll');
                    goods.refresh = mui('#scroll').pullRefresh({
                        down: {
                            callback: function () {
                                goods.page = 0;
                                self._refreshCb(goods);
                            },
                            auto: true
                        }, up: {
                            callback: function () {
                                self._refreshCb(goods);
                            }
                        }
                    });
                }
            };

            refreshData(0);
        },
        lazy:function () {
            this._super();
            setTimeout(function () {

                /*$('#allCheck').click(function () {
                    var goods = self.mData.orderSet['scroll'];
                    if($('#allCheck').is(':checked')){
                        $('#scroll input[type=checkbox]').prop('checked', true);
                        for(var i=0;i<goods.data.length;i++){
                            goods.data[i].checked = true;
                        }
                    }else{
                        $('#scroll input[type=checkbox]').prop('checked', false);
                        for(var i=0;i<goods.data.length;i++){
                            goods.data[i].checked = false;
                        }
                    }
                });

                $('#btn_bjsq').click(function() {
                    console.log(123)
                    self.productData.splice(0,self.productData.length);
                    var goods = self.mData.orderSet['scroll'];
                    for(var i=0;i<goods.data.length;i++){
                        if(goods.data[i].checked == true){
                            if(goods.data[i].MMRS008<=0){
                                app.toast('NO.'+(i+1)+'库存不足');
                                return;
                            }else{
                                self.productData.push(goods.data[i]);
                            }

                        }
                    }

                    if(self.productData.length == 0){
                        app.toast('没有选择的备件');
                    }else{
                        $(".modal1").css("display", "block");
                        mask.show();
                    }
                });*/
            }, 200);
        }
    });

});
